/**
 * Copyright 2012 Universita' degli Studi di Salerno


   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package it.isislab.dmason.experimentals.util.management.garbagecollector;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class Server extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public Server(Start s) {
                initComponents();
                starter = s;
        }

        private void initComponents() {
                // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
                // Generated using JFormDesigner Evaluation license - aaa aaa
                panel2 = new JPanel();
                scrollPane1 = new JScrollPane();
                textArea1 = new JTextArea();
                panel3 = new JPanel();
                label1 = new JLabel();
                textField1 = new JTextField();
                textField1.setText("127.0.0.1");
                label2 = new JLabel();
                textField2 = new JTextField();
                textField2.setText("8888");
                label3 = new JLabel();
                label3.setIcon(new ImageIcon(ClassLoader.getSystemClassLoader().getResource("it.isislab.dmason/resource/image/ok.png")));
                panel1 = new JPanel();
                toggleButton2 = new JButton();
                toggleButton4 = new JButton();
                toggleButton3 = new JButton();
                address = textField1.getText();
                port = textField2.getText();
                
                toggleButton2.addMouseListener(new MouseListener() {
                        
                        @Override
                        public void mouseReleased(MouseEvent arg0) {
                                starter.execute("start");
                                textArea1.setText("Starting ActiveMQ Broker...");
                        }
                        
                        @Override
                        public void mousePressed(MouseEvent arg0) {}
                        
                        @Override
                        public void mouseExited(MouseEvent arg0) {}
                        
                        @Override
                        public void mouseEntered(MouseEvent arg0) {}
                        
                        @Override
                        public void mouseClicked(MouseEvent arg0) {}
                });
                
                toggleButton3.addMouseListener(new MouseListener() {
                        
                        @Override
                        public void mouseReleased(MouseEvent e) {
                                        starter.execute("restart");
                                        textArea1.setText("Restarting ActiveMQ Broker...");
                        }
                        
                        @Override
                        public void mousePressed(MouseEvent e) {}
                        
                        @Override
                        public void mouseExited(MouseEvent e) {}
                        
                        @Override
                        public void mouseEntered(MouseEvent e) {}
                        
                        @Override
                        public void mouseClicked(MouseEvent e) {}
                });
                
                toggleButton4.addMouseListener(new MouseListener() {
                        
                        @Override
                        public void mouseReleased(MouseEvent e) {
                                        starter.execute("stop");
                                        textArea1.setText("Stopppig ActiveMQ Broker...");
                                        
                        }
                        
                        @Override
                        public void mousePressed(MouseEvent e) {}
                        
                        @Override
                        public void mouseExited(MouseEvent e) {}
                        
                        @Override
                        public void mouseEntered(MouseEvent e) {}
                        
                        @Override
                        public void mouseClicked(MouseEvent e) {}
                });
                label3.addMouseListener(new MouseListener() {
                        
                        @Override
                        public void mouseReleased(MouseEvent e) {
                                address = textField1.getText();
                                port = textField2.getText();
                                if(starter.connect(address,port))
                                        textArea1.setText("Connected...");
                                else 
                                        JOptionPane.showMessageDialog(null,"Wrong data!");
                        }
                        
                        @Override
                        public void mousePressed(MouseEvent e) {}
                        
                        @Override
                        public void mouseExited(MouseEvent e) {}
                        
                        @Override
                        public void mouseEntered(MouseEvent e) {}
                        
                        @Override
                        public void mouseClicked(MouseEvent e) {}
                });
                //======== this ========
                setTitle("Manager");
                setIconImage(new ImageIcon(ClassLoader.getSystemClassLoader().getResource("it.isislab.dmason/resource/image/connect.jpg")).getImage());
                Container contentPane = getContentPane();

                //======== panel2 ========
                {
                        panel2.setBorder(new TitledBorder("STATUS"));

                        //======== scrollPane1 ========
                        {
                                scrollPane1.setViewportView(textArea1);
                        }

                        GroupLayout panel2Layout = new GroupLayout(panel2);
                        panel2.setLayout(panel2Layout);
                        panel2Layout.setHorizontalGroup(
                                panel2Layout.createParallelGroup()
                                        .addGroup(panel2Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                                .addContainerGap())
                        );
                        panel2Layout.setVerticalGroup(
                                panel2Layout.createParallelGroup()
                                        .addGroup(panel2Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                                                .addContainerGap())
                        );
                }

                //======== panel3 ========
                {
                        panel3.setBorder(new EtchedBorder());

                        //---- label1 ----
                        label1.setText("IP  :");

                        //---- label2 ----
                        label2.setText("Port :");

                        GroupLayout panel3Layout = new GroupLayout(panel3);
                        panel3.setLayout(panel3Layout);
                        panel3Layout.setHorizontalGroup(
                                panel3Layout.createParallelGroup()
                                        .addGroup(panel3Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(label1)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(label2)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap(18, Short.MAX_VALUE))
                        );
                        panel3Layout.setVerticalGroup(
                                panel3Layout.createParallelGroup()
                                        .addGroup(panel3Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(panel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                        .addComponent(label1)
                                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label2)
                                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
                                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        );
                }

                //======== panel1 ========
                {
                        panel1.setBorder(new EtchedBorder());

                        //---- toggleButton2 ----
                        toggleButton2.setText("Start");

                        //---- toggleButton4 ----
                        toggleButton4.setText("Stop");

                        //---- toggleButton3 ----
                        toggleButton3.setText("Restart");

                        GroupLayout panel1Layout = new GroupLayout(panel1);
                        panel1.setLayout(panel1Layout);
                        panel1Layout.setHorizontalGroup(
                                panel1Layout.createParallelGroup()
                                        .addGroup(panel1Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(panel1Layout.createParallelGroup()
                                                        .addComponent(toggleButton2, GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                                                        .addComponent(toggleButton3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(toggleButton4, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                                                .addContainerGap())
                        );
                        panel1Layout.setVerticalGroup(
                                panel1Layout.createParallelGroup()
                                        .addGroup(panel1Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(toggleButton2)
                                                .addGap(18, 18, 18)
                                                .addComponent(toggleButton4)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                                                .addComponent(toggleButton3)
                                                .addContainerGap())
                        );
                }

                GroupLayout contentPaneLayout = new GroupLayout(contentPane);
                contentPane.setLayout(contentPaneLayout);
                contentPaneLayout.setHorizontalGroup(
                        contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                                .addComponent(panel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(panel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addContainerGap())
                );
                contentPaneLayout.setVerticalGroup(
                        contentPaneLayout.createParallelGroup()
                                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(panel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                                .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(panel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGap(35, 35, 35))
                );
                pack();
                setLocationRelativeTo(getOwner());
        }
        private JPanel panel2;
        private JScrollPane scrollPane1;
        private JTextArea textArea1;
        private JPanel panel3;
        private JLabel label1;
        private JTextField textField1;
        private JLabel label2;
        private JTextField textField2;
        private JLabel label3;
        private JPanel panel1;
        private JButton toggleButton2;
        private JButton toggleButton4;
        private JButton toggleButton3;
        private Start starter;
        private String address;
        private String port;
        
        public static void main(String[] args){
                JFrame s = new Server(new Start());
                s.setVisible(true);
        }

}